package com.jeeplus.sys.utils;


import cn.hutool.extra.spring.SpringUtil;
import com.jeeplus.common.redis.RedisUtils;
import com.jeeplus.sys.service.OfficeService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 缓存部门id 和名称
 * @Company：滨州学院
 * @Author:
 * @Date: 2021/8/24 11:45
 * @Version 1.0
 */
public class OfficeCacheUtils {
    private static final String OFFICE_CACHE = "officeCache";

    public static Map<String, String> getOfficeCache() {
        Map<String, String> OfficeMap = (Map<String, String>) RedisUtils.getInstance().get(OFFICE_CACHE);
        if (OfficeMap == null) {
            List<CacheVO> officeList = SpringUtil.getBean(OfficeService.class).findIdAndNameList();
            OfficeMap = officeList.stream().collect(Collectors.toMap(CacheVO::getId, CacheVO::getName));
        }
        RedisUtils.getInstance().set(OFFICE_CACHE, OfficeMap);
        return OfficeMap;
    }

    /**
     * @return
     * @Date 2021/8/25 10:28
     * @Author
     * @Description //TODO 获取部门名称
     **/
    public static String getOfficeName(String id) {
        Map<String, String> officeMap = getOfficeCache();
        return officeMap.get(id);
    }


    public static String getOfficeNames(String id) {
        String ids[] = id.split(",");
        Map<String, String> officeMap = getOfficeCache();
        String name = "";
        int length = ids.length;
        for (int i = 0; i < length; i++) {
            name += officeMap.get(ids[i]);
            if (i < length - 1) {
                name += ",";
            }
        }
        return name;
    }

    public static void clearCache() {
        RedisUtils.getInstance().delete(OFFICE_CACHE);
    }
}
